package com.harry.configuration.poentity.vo;

import com.harry.utility.PoEntityHelper;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class PropertyDescription {

	private String label;

	private String singularLabel;

	private String name;

	/**
	 * "bool" | "date" | "dict" | "oref" | "file" | "nmbr" | "pswd" | "txea" | "text";
	 */
	private String type;

	private String placeholder;

	private PoMode mode = new PoMode();

	private String orefType;

	private String declaringType;

	private Boolean izArray = false;

	private Boolean isSingleNameOref = false;

	private String dictGroupName;

	public void setDictGroupName(String dictGroupName) {
		this.type = "dict";
		this.dictGroupName = dictGroupName;
	}

	public void setType(String type) {
		this.type = type;
		String fieldName = this.name;
		Class<?> declaringClazz = PoEntityHelper.getTreeSetForClass().stream().filter(c -> c.getSimpleName().equals(this.declaringType)).findFirst().get();
		this.handleAbles(declaringClazz, fieldName);
	}

	public void setOrefType(String orefType) throws Exception {
		// for deserialize mapping null return
		if (orefType == null) return;
		setType("oref");
		this.orefType = orefType;
		Class<?> orefClazz = PoEntityHelper.getTreeSetForClass().stream().filter(c -> c.getSimpleName().equals(orefType)).findFirst().get();
		this.isSingleNameOref = PoEntityHelper.isSingleNameOref(orefClazz);
		Class<?> declaringClazz = PoEntityHelper.getTreeSetForClass().stream().filter(c -> c.getSimpleName().equals(this.declaringType)).findFirst().get();
		String fieldName = this.name;
		this.getMode().setCascadable(PoEntityHelper.isCascadable(declaringClazz, fieldName));
		this.handleAbles(declaringClazz, fieldName);
	}

	private void handleAbles(Class<?> declaringClazz, String fieldName) {
		if (!this.izArray) {
			this.getMode().setInsertable(PoEntityHelper.isInsertable(declaringClazz, fieldName));
			this.getMode().setNullable(PoEntityHelper.isNullable(declaringClazz, fieldName));
			this.getMode().setUpdatable(PoEntityHelper.isUpdatable(declaringClazz, fieldName));
		}
	}
}
